﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
using System.IO;
using System.Reflection;

namespace RPGLUA
{
    class RPG
    {
        Lua lua ;
        bool quit = false;
        Room currentRoom ;
        public RPG()
        {
            lua = new Lua();
            
                
            lua.RegisterFunction("CreateRoom", this, this.GetType().GetMethod("CreateRoom"));
            lua.RegisterFunction("CreateNPC", this, this.GetType().GetMethod("CreateNPC"));
            lua.RegisterFunction("CreatePassageWay", this, this.GetType().GetMethod("CreatePassageWay"));
            lua.RegisterFunction("AddNPCToRoom", this, this.GetType().GetMethod("AddNPCToRoom"));
            lua.RegisterFunction("SetStartRoom", this, this.GetType().GetMethod("SetStartRoom"));
            lua.DoFile("setup.lua");
        }
        static void Main(string[] args)
        {

            RPG rpg = new RPG();
            rpg.Run();
        }

        public void Run()
        {
            Console.WriteLine("Welcome to the cool RPG Game");
            Console.WriteLine("\tPress q to quit!");
            while (!quit)
            {
                Explore(currentRoom);
                Console.Write(">");
                string ans = Console.ReadLine();
                Console.Clear();

                currentRoom.CheckInteractions(ref currentRoom, ans);
                if (ans == "q")
                    quit = true;
            }
            Console.WriteLine("Goodbye");
        }

        public void Explore(Room r)
        {
            Console.WriteLine("You are in: " + r.Name);
            Console.WriteLine(r.Description);
            r.DescribeOccupants();
            Console.WriteLine();
            r.DisplayInteractions();
        }

        public Room CreateRoom(string roomName)
        {
            return new Room(roomName, lua);
        }

        public NPC CreateNPC(string npcName)
        {
            return new NPC(npcName, lua);
        }

        public PassageWay CreatePassageWay(string describe, Room r)
        {
            return new PassageWay(describe, r);
        }

        public void SetStartRoom(Room r)
        {
            currentRoom = r;
        }

        public void AddNPCToRoom(Room r, NPC n)
        {
            r.AddNPC(n);
        }

    }
}
